home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / art&graf.ix / art-0015 / flicker / memory.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  4KB  |  243 lines

  1.  
  2. #include <osbind.h>
  3.  
  4. #define NULL 0L    
  5.  
  6. struct mblock
  7.     {
  8.     struct mblock *next;
  9.     long size;
  10.     };
  11.  
  12. static struct mblock *free_list = NULL;
  13. long cel_mem_alloc;
  14. long mem_free;
  15.  
  16. static
  17. not_enough()
  18. {
  19. puts("not enough memory to run, sorry");
  20. }
  21.  
  22. init_mem()
  23. {
  24. register long size;
  25. char *pool;
  26. char *sc1, *sc2, *sc3;
  27.  
  28. size = Malloc((long)-1);    /*see what's available */
  29. size -= 24*1024;           /*leave 24 for GEM */
  30. if (size < 96000L)        /*if not enough left abort flicker */
  31.     {
  32.     not_enough();
  33.     return(0);
  34.     }
  35. if ( (pool = (char  *)Malloc(size)) == NULL)
  36.     {
  37.     not_enough();
  38.     return(0);
  39.     }
  40. free_list = (struct mblock *)pool;
  41. free_list->next = NULL;
  42. mem_free = free_list->size = size;
  43. return(1);
  44. }
  45.  
  46.  
  47. #ifdef SLUFFED
  48. dump_frags()
  49. {
  50. register struct mblock *mb= free_list;
  51.  
  52. while (mb)
  53.     {
  54.     printf("%lx %lx\n", mb, mb->size);
  55.     mb = mb->next;
  56.     }
  57. }
  58. #endif SLUFFED
  59.  
  60. int *
  61. alloc(nbytes)
  62. register int nbytes;
  63. {
  64. register struct mblock *mb, *nb, *lb;
  65.  
  66. #ifdef DEBUG
  67. printf("alloc(%d)\n", nbytes);
  68. #endif DEBUG
  69.  
  70. if (nbytes <= 0)
  71.     return(NULL);
  72.  
  73. nbytes = (nbytes+7) & 0xfff8;
  74.  
  75. if (mb = free_list)
  76.     {
  77.     if (mb->size == nbytes)
  78.         {
  79. #ifdef DEBUG1
  80.         printf("alloc first exact %lx\n", mb);
  81. #endif DEBUG1
  82.         free_list = mb->next;
  83.         mem_free -= nbytes;
  84.         return((int *)mb);
  85.         }
  86.     else if (mb->size > nbytes)
  87.         {
  88. #ifdef DEBUG1
  89.         printf("alloc first %lx\n", mb);
  90. #endif DEBUG1
  91.         nb = (struct mblock *)(((char *)mb)+nbytes);
  92.         nb->next = mb->next;
  93.         nb->size = mb->size - (long)nbytes;
  94.         free_list = nb;
  95.         mem_free -= nbytes;
  96.         return((int *)mb);
  97.         }
  98.     else
  99.         {
  100.         lb = mb;
  101.         mb = mb->next;
  102.         }
  103.     }
  104.  
  105. while (mb)
  106.     {
  107.     if (mb->size == nbytes)
  108.         {
  109. #ifdef DEBUG1
  110.         printf("alloc exact %lx\n", mb);
  111. #endif DEBUG1
  112.         lb->next = mb->next;
  113.         mem_free -= nbytes;
  114.         return((int *)mb);
  115.         }
  116.     else if (mb->size > nbytes)
  117.         {
  118. #ifdef DEBUG1
  119.         printf("alloc middle %lx\n", mb);
  120. #endif DEBUG1
  121.         nb = (struct mblock *)(((char *)mb)+nbytes);
  122.         nb->next = mb->next;
  123.         nb->size = mb->size - (long)nbytes;
  124.         lb->next = nb;
  125.         mem_free -= nbytes;
  126.         return((int *)mb);
  127.         }
  128.     else
  129.         {
  130.         lb = mb;
  131.         mb = mb->next;
  132.         }
  133.     }
  134. return(NULL);
  135. }
  136.  
  137.  
  138. mfree(nb, amount)
  139. struct mblock *nb;
  140. register int amount;
  141. {
  142. register struct mblock *mb;
  143. register struct mblock *lb;
  144.  
  145. #ifdef DEBUG
  146. printf("mfree(%lx, %d)\n", nb, amount);
  147. #endif DEBUG
  148.  
  149. if (amount <= 0)
  150.     return;
  151.  
  152. amount = (amount+7)&0xfff8;
  153. mem_free += amount;
  154.  
  155. if ( (mb = free_list) == NULL)
  156.     {
  157. #ifdef DEBUG1
  158.     printf("new free_list\n");
  159. #endif DEBUG1
  160.     mb = free_list = nb;
  161.     mb->next = NULL;
  162.     mb->size = amount;
  163.     return;
  164.     }
  165. if ( nb < mb)
  166.     {
  167.     free_list = nb;
  168.     nb->next = mb;
  169.     nb->size = amount;
  170.     if ( (char *)nb+amount == (char *)mb)    /*coalesce into first block*/
  171.         {
  172.         nb->next = mb->next;
  173.         nb->size += mb->size;
  174. #ifdef DEBUG1
  175.         printf("coalescing into first chunk\n");
  176. #endif DEBUG1
  177.         }        
  178. #ifdef DEBUG1
  179.     else
  180.         printf("new first chunk\n");    
  181. #endif DEBUG1
  182.     return;
  183.     }
  184. for (;;)
  185.     {
  186.     lb = mb;
  187.     if ( (mb = mb->next) == NULL)
  188.         break;
  189.     if ((char *)nb - lb->size == (char *)lb)    /*coalesce into previous block*/
  190.         {
  191.         lb->size += amount;
  192.         if ((char *)nb + amount == (char *)mb)
  193.             {
  194.             lb->size += mb->size;
  195.             lb->next = mb->next;
  196. #ifdef DEBUG1
  197.             printf("coalescing both sides\n");
  198. #endif DEBUG1
  199.             }
  200. #ifdef DEBUG1
  201.         else
  202.             printf("coalescing into previous block\n");
  203. #endif DEBUG1
  204.         return;
  205.         }
  206.     if ((char *)nb+amount == (char *)mb)    /*coalesce into next block*/
  207.         {
  208.         nb->size = mb->size + amount;
  209.         nb->next = mb->next;
  210.         lb->next = nb;
  211. #ifdef DEBUG1
  212.         printf("coalescing into next block\n");
  213. #endif DEBUG1
  214.         return;
  215.         }
  216.     if (nb < mb)
  217.         {
  218. #ifdef DEBUG1
  219.         printf("adding block in middle\n");
  220. #endif DEBUG1
  221.         nb->next = mb;
  222.         lb->next = nb;
  223.         nb->size = amount;
  224.         return;
  225.         }
  226.     }
  227. if ((char *)nb-lb->size == (char *)lb)    /*a rare case ... */
  228.     {
  229. #ifdef DEBUG1
  230.     printf("coalescing into end of last block\n");
  231. #endif DEBUG1
  232.     lb->size += amount;
  233.     return;
  234.     }
  235. #ifdef DEBUG1
  236. printf("adding last block\n");
  237. #endif DEBUG1
  238. lb->next = nb;
  239. nb->next = NULL;
  240. nb->size = amount;
  241. }
  242.  
  243.